package org.feng.aop;

import lombok.extern.slf4j.Slf4j;
import org.apache.poi.util.StringUtil;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @author 冯帅
 * @des TODO
 * @date 2021/9/28 15:22
 * @email 15612509687@163.com
 */
@Aspect
@Component
@Slf4j
public class ApiAop {
    private Boolean flag=true;

    @Pointcut("@within(org.springframework.web.bind.annotation.RestController)")
    public void apiAop() {
    }

    /**
     * 日期解析aop
     *
     * @param pjp
     * @return
     * @throws Throwable
     */
    @Around("apiAop()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable {
        if (!flag) {
            return pjp.proceed();
        }
        long start = System.currentTimeMillis();
        long end = 0L;
        Object result = null;
        String className = "";
        String methodName = "";
        try {
            className = pjp.getTarget().getClass().getName();
            MethodSignature ms = (MethodSignature) pjp.getSignature();
            methodName = ms.getMethod().getName();
            result = pjp.proceed();
            end = System.currentTimeMillis();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        } finally {
            log.info("\n--------------------接口分析--------------------\n" +
                    "类名     ：" + className + "\n" +
                    "方法     ：" + methodName + "\n" +
                    "执行时间 ：" + (end - start) + "ms");
        }
    }

}
